package controller

import (
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"good.house/internal/dto/request"
	"good.house/internal/service"
	innerValidator "good.house/internal/validator"
	"good.house/pkg/response"
)

type ListController struct {
	service *service.ListService
}

func NewListController(service *service.ListService) *ListController {
	return &ListController{
		service: service,
	}
}

// @Summary 租房列表
// @Description 租房列表
// @Tags 信息列表
// @Accept  json
// @Produce  json
// @Param    data  body  request.ListRequest  true  "租房列表"
// @Success 200 {object}  response.ListResponse
// @Router /api/getRentList [post]
func (ctl *ListController) GetRentList(ctx *gin.Context) {
	var req request.ListRequest

	if err := ctx.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		if errs, ok := innerValidator.TranslateValidateError(err); ok {
			response.Error(ctx, strings.Join(errs, ";"))
			return
		}
		response.Error(ctx, err.Error())
		return
	}

	dataList, err := ctl.service.GetList(&req)

	if err != nil {
		response.Error(ctx, err.Error())
	} else {
		response.Success(ctx, "成功", dataList)
	}
}
